﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Commands.User.Commands.Order;
using DotNet.Common.Command;
using System.Collections;
using ZLCMS.Data.Model;
using System.IO;
using ZLCMS.Commands.User.Util;
using NHibernate;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Order
{
    public partial class OrderRefundList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ分页控件
            _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
            LoadOrderRefundListParameter param = new LoadOrderRefundListParameter();

            param.ConfirmedStatus = string.IsNullOrEmpty(Request["confirm"]) ? "-1" : Request["confirm"];
            ParamKit.Bind("OrderIsConfirmed", confirm, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, param.ConfirmedStatus, "-1");

            param.ProcessStatus = string.IsNullOrEmpty(Request["process"]) ? "-1" : Request["process"];
            ParamKit.Bind("ProcessStatus", process, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, param.ProcessStatus, "-1");

            param.FinishedStatus = string.IsNullOrEmpty(Request["finish"]) ? "-1" : Request["finish"];
            ParamKit.Bind("OrderIsFinish", finish, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, param.FinishedStatus, "-1");

            param.CancelStatus = string.IsNullOrEmpty(Request["cancel"]) ? "-1" : Request["cancel"];
            ParamKit.Bind("OrderIsCancel", cancel, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, param.CancelStatus, "-1");
           
            param.OrderNo = string.IsNullOrEmpty(Request["orderno"]) ? "" : Request["orderno"];
            orderno.Text = param.OrderNo;
            param.ProductName = string.IsNullOrEmpty(Request["pname"]) ? "" : Request["pname"];
            pName.Text = param.ProductName;

            param.DeptCode = "";
            if (AuthUtil.CheckAuthorization("ORDER_RETURN_MAN", "VIEW_ALL"))
            {
            }
            else if (AuthUtil.CheckAuthorization("ORDER_RETURN_MAN", "VIEW_OWNER"))
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    param.DeptCode = session.CreateSQLQuery("select dept_code from sys_user_info where user_account=:acc")
                        .SetParameter("acc", UserKit.GetUserAccountFailLogin()).UniqueResult<string>();
                    //param.UserAccount = UserKit.GetUserAccount();
                }
            }
            else
            {
                //没有权限访问
                param.DeptCode = "no_dept";
            }

            if (!string.IsNullOrEmpty(Request["exportexcel"]))
            {
                ICommandResult<LoadOrderRefundListResult> result = new LoadOrderRefundListCommand().Execute(param);
                IList<IDictionary> list = result.Result.List;

                //StringWriter sw = new StringWriter();
                //sw.WriteLine("订单号\t商品编号\t商品名称\t记录时间\t重量\t退回价格\t订单退货类型\t退还理由\t退货状态\t申请人\t");

                //foreach (var item in list)
                //{
                //    sw.WriteLine(item["OrderNo"] + "\t" + item["ProductNo"] + "\t" + item["ProductName"] + "\t" + item["RecodeDate"]
                //        + "\t" + item["Quantity"] + "\t" + item["TotalPrice"] + "\t" + item.ReturnType + "\t" + item.ReturnMemos + "\t"  
                //        + ParamKit.GetValue("OrderIsConfirmed", item.ConfirmStatus, "") + ParamKit.GetValue("ProcessStatus", item.ProcessStatus, "")
                //        + ParamKit.GetValue("OrderIsFinish", item.FinishStatus, "") + ParamKit.GetValue("OrderIsCancel", item.CancelStatus, "") + "\t" + item.MemberAccount + "\t");
                //}
                //sw.Close();
                //OutputKit.OutputExcel(Response, sw);
            }
            else
            {
                //设置分页大小

                param.PageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;
                //设置页码
                param.PageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;

                ICommandResult<LoadOrderRefundListResult> result = new LoadOrderRefundListCommand().Execute(param);
                grid.DataSource = result.Result.List;
                grid.DataBind();

                _framePage.RecordCount = result.TotalCount;
            }
        }
    }
}